package lec08.am;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class KeyWordCounter {
	static Map<String, Integer> map = new TreeMap<String, Integer>();
	static {
		try {
			Scanner cin = new Scanner(new File("keywords.txt"));
			while (cin.hasNext())
				map.put(cin.next(), 0);
			cin.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		String fileName = "F:/eqJava/works/coreJava/src/zj/game/tictactoe2/TicTacToe.java";
		counter(fileName);
		disp();
		Map<Integer, Set<String>> map2 = new TreeMap<Integer, Set<String>>();
		for (String k : map.keySet()) {
			int newKey = map.get(k);
			if (map2.containsKey(newKey)) {
					Set<String> curSet=map2.get(newKey);
					curSet.add(k);
			} else {
				TreeSet<String> s=new TreeSet<String>();
				s.add(k);
				map2.put(newKey, s);
			}
		}
		System.out.println(map);
		System.out.println(map2);
	}

	private static void disp() {
		for (String k : map.keySet())
			System.out.println(k + ":" + map.get(k));

	}

	private static void counter(String fileName) {

		try {
			Scanner cin = new Scanner(new File(fileName));
			while (cin.hasNextLine()) {
				String line = cin.nextLine().trim();
				String ss[] = line.split("\\W+");
				for (String s : ss) {
					if (map.containsKey(s))
						map.put(s, map.get(s) + 1);
				}
			}
			cin.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
